package similarity;

import java.util.HashMap;
import java.util.Random;

import randomwalk.Submatrix;

//number of independent LshHashFunction
/**
 * @author  user
 */
public class LshProjection {

	//two hash functions instead of storing tuples with projections
	private int [] storageHash1;
	private int [] storageHash2;
	
	/**
	 * @uml.property  name="hashFunctions"
	 * @uml.associationEnd  multiplicity="(0 -1)"
	 */
	private LshHashFunction [] hashFunctions;
	
	public LshProjection(int hashFunctionsNumber, 
			double bucketWidth, int vectorLength) {
		hashFunctions = new LshHashFunction [hashFunctionsNumber];
		storageHash1 = new int[vectorLength];
		storageHash2 = new int[vectorLength];
		
		for (int i = 0; i < hashFunctionsNumber; i++) {
			hashFunctions[i] = new LshHashFunction(bucketWidth, vectorLength);
		}
	}
	
	public void calculateHashes(Submatrix submatrix, int resultLength) {
		for (LshHashFunction hash : hashFunctions) {
			int [] usersHash = hash.calculateUserHash(submatrix, resultLength);
			
			Random randMulripliersGen = new Random();
			int randomMultiplier1 = randMulripliersGen.nextInt();
			int randomMultiplier2 = randMulripliersGen.nextInt();
			
			for (int i = 0; i < usersHash.length; i++) {
				storageHash1[i] += usersHash[i]*randomMultiplier1;
				storageHash2[i] += usersHash[i]*randomMultiplier2;
			}
		}
		System.out.println("!");
		checkHashes();
	}
	
	public void checkHashes() {
		HashMap<Integer,Integer> map = new HashMap<Integer,Integer>();
		for (int i = 0; i < storageHash1.length; i++) {
			if(map.containsKey(storageHash1[i]) & (storageHash1[i] != 0)) {
				if(storageHash2[i] == storageHash2[map.get(storageHash1[i])]) {
					System.out.println(i + ","+ map.get(storageHash1[i]));
				}
			}
			else {
				map.put(storageHash1[i], i);
			}
		}
		
	}
}
